var json2 = require('../../json/json2.js');
var app=getApp();
Page({
  onLoad(){
    this.setData({ myicons: json2.myicons, mylists: json2.mylists});
    //this.setData({ 'mylists[1].text': '3张可用', 'mylists[2].text':'已认证'})//设置数组内部数据
  },
  onShow(){
    let token = wx.getStorageSync('token');
    if (token === ""){//判断登陆状态
      this.setData({ login: false, index: this.data.index + 1});
      if (this.data.index == 1) {
        wx.navigateTo({ url: '/pages/login/login' })
      }
    }else{
      let nickname = wx.getStorageSync('userdata').nickname;
      this.setData({ login:true,nickname:nickname});
      this.zhima();
      app.getshelfnum();
      this.orderstatus();
    }
  },
  data:{
    login:false,
    avatarurl:"/img/shouye/logo.png",
    myicons:[],
    mylists:[],
    index:0
  },
  login(e){
    if (app.repeat(e.timeStamp) == false) { return; }
    wx.navigateTo({ url: '../login/login'})
  },
  remove(){
    let _this=this;
    wx.showModal({
      title: '提示',
      content: '退出登录?',
      success(res){
        if(res.confirm){
          wx.removeStorageSync('token');
          wx.removeStorageSync('userdata');
          _this.onShow();
        }
      }
    })
  },
  list2(e){
    if (app.repeat(e.timeStamp) == false) { return; }
    let id=e.currentTarget.id;
    if(id=='我的会员'){
      wx.navigateTo({ url: '/pages/home/mycard/mycard' })
    } else if (id == '优惠券'){
      wx.navigateTo({ url: '/pages/home/coupon/coupon' })
    } else if(id=='芝麻信用'){
      wx.navigateTo({ url: '/pages/home/zhima/zhima' })
    } else if (id == '收货地址') {
      wx.navigateTo({ url: '/pages/home/addresslist/addresslist?from=set' })
    } else if(id=='设置'){
      wx.navigateTo({ url: '/pages/set/set'})
    } else if (id == '宝贝书屋') {
      wx.navigateTo({ url: '/pages/babyhouse/babyhouse?from=my' })
    } else if (id == '砍价列表') {
      wx.navigateTo({ url: '/pages/kanjia/kanjiarecord/kanjiarecord' })
    }
  },
  orderstatus(){//获取订单状态
    let token = wx.getStorageSync('token');
    let user_id = wx.getStorageSync('userdata').user_id;
    app.request('book/order/getOrderCount', 'POST', { token: token, user_id: user_id },'orderstatusb',this)
  },
  orderstatusb(data){
    if(data.code!=200){return}
    let myicons = this.data.myicons;
    data.data.forEach((item,index)=>{
      myicons[index + 1].num = item;
    })
    this.setData({ myicons: myicons})
  },
  order(e){
    if (app.repeat(e.timeStamp) == false) { return; }
    let id=e.currentTarget.id;
    wx.navigateTo({url: '/pages/order/order?id='+id})
  },
  zhima(){//芝麻是否授权
    let zhimasq=wx.getStorageSync('zhimasq');
    let param = new Object();
    if (zhimasq.status=="0"){
      param['mylists[4].text']='去授权';
    }else{
      // if(zhimasq.erdu>0){
      //   param['mylists[2].text'] = '免押金额度' + zhimasq.erdu+'元';
      // }else{
        param['mylists[4].text'] = '已授权'
      // }
    }
    this.setData(param);
  },
  babyinfo(){
    wx.navigateTo({url:'/pages/babyinfo/babyinfo'})
  },
  news(){
    wx.navigateTo({ url: '/pages/news/news' })
  }
})